// TypeScript file
module starlingswf
{
export class Swf
{
    public static dataKey_Sprite: string = "spr";
    public static dataKey_Image: string = "img";
    public static dataKey_MovieClip: string = "mc";
    public static dataKey_TextField: string = "text";
    public static dataKey_Button: string = "btn";
    public static dataKey_Scale9: string = "s9";
    public static dataKey_ShapeImg: string = "shapeImg";
    public static dataKey_Component: string = "comp";
    public static dataKey_Particle: string = "particle";

    private createDisplayFuns: Object;

    private swfData: Object;

    private assetManager: starlingswf.SwfAssetManager;

    public swfUpdateManager: starlingswf.SwfUpdateManager;
    public name: string;

    constructor(swfData: Object, assetManager: SwfAssetManager, fps: number = 24)
    {
        this.swfData = swfData;
        this.assetManager = assetManager;
        this.createDisplayFuns = new Object();
        this.createDisplayFuns[Swf.dataKey_Sprite] = this.createSprite;
        this.createDisplayFuns[Swf.dataKey_MovieClip] = this.createMovie;
        this.createDisplayFuns[Swf.dataKey_Image] = this.createImage;
        this.createDisplayFuns[Swf.dataKey_Scale9] = this.createS9Image;
        this.createDisplayFuns[Swf.dataKey_ShapeImg] = this.createShapeImage;
        this.createDisplayFuns[Swf.dataKey_TextField] = this.createTextField;

        this.swfUpdateManager = SwfUpdateManager.create(fps);
    }

    public createSprite(name: string, data: any[] = null, sprData: any[] = null): SwfSprite
    {
        if (null == sprData)
        {
            sprData = this.swfData[Swf.dataKey_Sprite][name];
        }

        let sprite: SwfSprite = new SwfSprite();
        let length: number = sprData.length;
        let objData: any[];
        let display: egret.DisplayObject;
        let fun: Function;
        let swf: Swf;

        for (let i: number = 0; i < length; ++i)
        {
            objData = sprData[i];

            fun = this.createDisplayFuns[objData[1]];
            if (null == fun) continue;

            display = fun.apply(this, [objData[0], objData]);
            display.name = objData[9];
            display.x = objData[2];
            display.y = objData[3];

            if (objData[0] == Swf.dataKey_TextField)
            {
                //
            }

            if (objData[1] != Swf.dataKey_Scale9 && objData[1] != Swf.dataKey_ShapeImg)
            {
                display.scaleX = objData[4];
                display.scaleY = objData[5];
            }
            display.skewX = objData[6];
            display.skewY = objData[7];
            display.alpha = objData[8];
            sprite.addChild(display);
        }
        return sprite;
    }

    public createMovie(name: string, data:any[] = null, cls: any = null): SwfMovieClip
    {
        let movieClipData: Object = this.swfData[Swf.dataKey_MovieClip][name];
        let objectCountData: Object = movieClipData["objCount"];
        let displayObjects:Object = {};
        let displayObjectArray: any[];
        let type: string;
        let count: number;
        let fun: Function;
        let objName: string;

        for (objName in objectCountData)
        {
            type = objectCountData[objName][0];
            count = objectCountData[objName][1];
            displayObjectArray = displayObjects[objName] == null ? []  : displayObjects[objName];

            for (let i: number = 0; i < count; ++i)
            {
                fun = this.createDisplayFuns[type];
                if (null == fun) continue;

                displayObjectArray.push(fun.apply(this, [objName, null]));
            }
            displayObjects[objName] = displayObjectArray;
        }

        let mc: SwfMovieClip;
        if (null == cls)
        {
            mc = new SwfMovieClip(movieClipData["frames"], movieClipData["labels"], displayObjects, this);
        }
        else
        {
            mc = new cls(movieClipData["frames"], movieClipData["labels"], displayObjects, this);
        }
        mc.isLoop = movieClipData["loop"];
        return mc;
    }

    public createImage(name: string, data: any[] = null, cls: any = null): egret.Bitmap
    {
        let imageData: number[] = this.swfData[Swf.dataKey_Image][name];
        let bitmap: egret.Bitmap = this.assetManager.createBitmap(name);
        bitmap.anchorOffsetX = imageData[0];
        bitmap.anchorOffsetY = imageData[1];
        return bitmap;
    }

    public getTexture(name: string): egret.Texture
    {
        return this.assetManager.getTexture(name);
    }

    public createS9Image(name: string, data: any[] = null): egret.Bitmap
    {
        let s9Data: any[] = this.swfData[Swf.dataKey_Scale9][name];
        let bitmap: egret.Bitmap = this.assetManager.createBitmap(name);
        bitmap.scale9Grid = new egret.Rectangle(s9Data[0],
        s9Data[1], s9Data[2], s9Data[3]);

        if (null != data)
        {
            bitmap.width = <number>data[10];
            bitmap.height = <number>data[11];
        }        

        return bitmap;
    }

    public createShapeImage(name: string, data: any[] = null): egret.Bitmap
    {
        let bitmap: egret.Bitmap = this.assetManager.createBitmap(name);
        bitmap.fillMode = egret.BitmapFillMode.REPEAT;
        if (null != data)
        {
            bitmap.width = <number>data[10];
            bitmap.height = <number>data[11];
        }

        return bitmap;
    }

    public createTextField(name: string, data: any[] = null): egret.TextField
    {
        let textfield: egret.TextField = new egret.TextField();
        if (null == data)
        {
            return textfield;
        }
        textfield.width = <number>data[10];
        textfield.height = <number>data[11];
        textfield.textColor = <number>data[13];
        textfield.size = <number>data[14];
        textfield.textAlign = <string>data[15];
        textfield.text = <string>data[18];
        return textfield;
    }

    public hasSprite(name: string): boolean
    {
        return this.swfData[Swf.dataKey_Sprite][name] != null;
    }

    public hasMovieClip(name: string): boolean
    {
        return this.swfData[Swf.dataKey_MovieClip][name] != null;
    }

    public hasImage(name: string): boolean
    {
        return this.swfData[Swf.dataKey_Image][name] != null;
    }

    public hasS9Image(name: string): boolean
    {
        return this.swfData[Swf.dataKey_Scale9][name] != null;
    }

    public hasShapeImage(name: string): boolean
    {
        return this.swfData[Swf.dataKey_ShapeImg][name] != null;
    }
}
}//end of module starlingswf